package net.bingosoft.common;


/**
 * 2008-4-22 下午04:49:53
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:变量解析类
 * ChangeLog:
 */
public class VariableParser {
	
	private VariableParser(){}
	/**
	 * 获取操作系统的环境变量
	 * @param key 环境变量的键值
	 * @return
	 */
	public static String getOSVariable(String key){
		return System.getenv(key);
	}
	
	/** 
	 * 解析变量
	 * @param content 待解析的内容字符串
     * @param variablePrefix 环境变量前缀
     * @param variableSurfix 环境变量的后缀
     * @return 解析之后的内容
	 */
	public static String parseVariable(String content,IVariableProvider p,char variablePrefix,char variableSurfix){
		if(content == null ) return null;
		int start = content.indexOf(variablePrefix);
		if(start < 0) return content;
		int end = content.indexOf(variableSurfix, start+1);
		while(start > 0){
			String name = content.substring(start + 1,end);
			content = content.replace(variablePrefix + name + variableSurfix, p.getVariable(name));
			start = content.indexOf(variablePrefix);
			end = content.indexOf(variableSurfix,start+1);
		}
		return content;
	}
	/**
	 * Feb 11, 2009 1:13:09 PM
	 * Author:yangdongyu(yangdy@bingosoft.net)
	 * ReadMe:变量提供接口
	 * ChangeLog:
	 */
	public static interface IVariableProvider{
		/**
		 * 根据指定的key值获取对应的变量
		 * @param key
		 * @return
		 */
		String getVariable(String key);
	}
	
	

}
